package com.lanou.realm;


import com.lanou.bean.AdminInfo;
import com.lanou.bean.ModuleInfo;
import com.lanou.bean.RoleInfo;
import com.lanou.service.AdminInfoService;
import com.lanou.service.ModuleInfoService;
import com.lanou.service.RoleInfoService;
import com.lanou.service.impl.AdminInfoServiceImpl;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/7.
 */
public class MyPerRealm extends AuthorizingRealm {
    @Resource
    private AdminInfoService adminInfoService;
    @Resource
    private RoleInfoService roleInfoService;
    @Resource
    private ModuleInfoService moduleInfoService;
    @Override
    public String getName() {
        return "MyPerRealm";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("开始授权!!!!!!");
        //授权的方法
        List<String> rolesList = new ArrayList<>();
        List<String> moduleList = new ArrayList<>();
        //将认证方法中的返回的对象取出
        AdminInfo adminInfo = (AdminInfo) principalCollection.getPrimaryPrincipal();
        //角色
        List<RoleInfo> roleInfoList = roleInfoService.selectRoles(adminInfo.getName());

        System.out.println("角色"+roleInfoList);
        for (int i = 0; i <roleInfoList.size() ; i++) {
            String name = roleInfoList.get(i).getName();
            rolesList.add(name);
        }
        //权限
        List<ModuleInfo> moduleInfoList = moduleInfoService.selectModule(adminInfo.getName());
        System.out.println("权限"+moduleInfoList);

        for (ModuleInfo moduleInfo:moduleInfoList){
            moduleList.add(moduleInfo.getName());
        }

        //授权信息对象,用来保存所有的角色和权限信息
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addStringPermissions(moduleList);
        info.addRoles(rolesList);


        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("开始认证!!!!!!!");
        //1.获取token中的用户名
        String username = (String) authenticationToken.getPrincipal();

        System.out.println(username);

        AdminInfo adminInfo =adminInfoService.selectAdmin(username);
       if (adminInfo == null){
           throw new UnknownAccountException("没有找到账号");
       }
        //如果账号存在,接着判断密码
        String password = new String((char[]) authenticationToken.getCredentials());

        if (!adminInfo.getPassword().equals(password)) {
            throw new UnknownAccountException("密码错误");
        }

        return new SimpleAuthenticationInfo(adminInfo, password, getName());
    }
}
